<?php

namespace Service\RpcService\RpcJavaGateWayServer;
use Service\Code\Normal;
use Service\RpcService\BaseSocketClientService;
use Service\ServiceInterface\TcpSocketClientInterface;
use App\Service\AppService\SystemCacheConfigService;

/**
 * BBC java服务socket服务中心
 * @author chenshubo
 */
class ClientService extends BaseSocketClientService implements TcpSocketClientInterface
{
    protected $gateway_url  = 'http://127.0.0.1:3280';
    
    /**
     * 向服务端发起socket请求.
     * @param string $method
     * @param array $argments
     * @return mixed
     */
    public function send( string $method , array $argments = [], string $request_method = 'get')
    {
        $config = SystemCacheConfigService::instance()->getCacheInfoBySystemCode('bbcconfig');
        $BbcOpenUrl = $config['NewBbcOpenUrl'] ?? $this->gateway_url;
        //从配置里面里面处理地址，默认使用代码属性地址
        $config_gate_way = config('api.bbc_java_gateway_url');
        $this->gateway_url = $config_gate_way ? $config_gate_way : $BbcOpenUrl;
        $routesMap = RpcRoutesMap::$routesMap;
        if( !isset($routesMap[$method]) ) {
            return Normal::returnCode(Normal::FAIL,"rpc socket api 【{$method}】 不存在，请核对！");
        }
        return (new RpcJavaGateWayHttpClient($this->gateway_url))->{$request_method}($routesMap[$method],$argments);
    }
}